-- submit_to_tree_house
-- Created by dengc March/11/2016
-- 成就：向森树屋提交物品的相关

-- 内部函数声明
local whenSubmit;

-- 关注森の树屋提交事件
EventMgr.register("achievements/submit_to_tree_house", event.SUBMIT_TO_TREE_HOUSE, function(args)
    whenSubmit(args);
end);

-- 激活事件的回调
whenSubmit = function(args)
    local classId = args["classId"];

    -- 玩家不在地牢中
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 检查该物品以前是否提交过
    local submitInHistory = TreeHouseM.querSubmitInHistory() or {};
    for _, c in pairs(submitInHistory) do
        if c == classId then
            return;
        end
    end

    -- 记录下
    table.insert(submitInHistory, classId);
    TreeHouseM.setSubmitInHistory(submitInHistory);

    -- 获取当前所在的地牢区域
    local area = DungeonM.getDungeonId();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("submit_to_tree_house");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, area) then
            -- 不是该迷宫的成就
            return false;
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};
